// pages/infomation/infomation.js
import {
  ApiRootUrl
} from '../../config/index.js'
import Dialog from '../../@vant/weapp/dialog/dialog';
import Toast from '../../@vant/weapp/toast/toast';
import Notify from '../../@vant/weapp/notify/notify';
import {
  IMG_PATH
} from '../../config/index.js'
import {
  judgeData,isChinese
} from "../../utils/util"
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tishiShow: false,
    tabHeight:0,
    familyList: [{
      statusBarHeight: '',
      id: new Date().getTime(),
      name: '',
      relation: '请选择',
      birthday: '请选择',
      organization: '',
      job: '',
      phone: ''
    }],
    currentDate: new Date().getTime(),
    minDate: new Date(1920, 1, 1).getTime(),
    maxDate: new Date(2050, 1, 1).getTime(),
    maxDate1: new Date().getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}`;
      } else if (type === 'month') {
        return `${value}`;
      }
      return value;
    },
    educateList: [{
      id: new Date().getTime(),
      name: '',
      starttime: '请选择',
      endtime: '请选择',
      qualifications: '请选择',
      special: '',
      remark: '',
    }],
    workList: [{
      id: new Date().getTime(),
      name: '',
      starttime: '请选择',
      endtime: '请选择',
      qualifications: '请选择',
      special: '',
      reason: '',
    }],
    marryShow: false,
    marryColumns: ['未婚', '已婚'],
    isFriendShow: false,
    placeplaceShow: false,
    birthdaytime: '',
    birthdayShow: false,
    nationShow: false,
    isStayShow: false,
    submitFlag: true,
    familyinfo: {
      name: '',
      sex: '请选择',
      relation: '请选择',
      phone: ''
    },
    baseinfo: {
      currentspace: '',
      username: '',
      email: '',
      sex: '',
      phone: '',
      identifycode: '',
      birthday: '',
      nation: '',
      placeplace: '',
      isStay: '否',
      isFriend: '无',
      friendName: '',
      friendPhone: '',
      household: '',
      marry: '未婚'
    },
    active: 0,
    promiseRadio: false,
    placeplaceColumns: [],
    tabList: [{
      title: '基础信息',
      disabled: false,
    }, {
      title: '家庭信息',
      disabled: true,
    }, {
      title: '学历信息',
      disabled: true,
    }, {
      title: '工作经历',
      disabled: true,
    }],
    isStayColumns: ['是', '否'],
    nationColumns: ['汉族', '藏族', '维吾尔族', '朝鲜族'],
    nationColumnsList: [],
    isFriendColumns: ['有', '无'],
    sexColumns: ['男', '女'],
    sexShow: false,
    relationShow: false,
    relationColumns: ['父母', '配偶', '子女', '兄弟姐妹'],
    relationColumnsObj: {
      '父母': '0',
      '配偶': '1',
      '子女': '2',
      '兄弟姐妹': '3',
    },
    familyrelationShow: false,
    currentId: '',
    familybirthdayShow: false,
    basesexShow: false,
    educatestartShow: false,
    educateendShow: false,
    workstartShow: false,
    workendShow: false,
    qualificationsShow: false,
    workqualificationsShow: false,
    workqualificationsColumns: ['是', '否'],
    qualificationsColumns: ['本科', '大专', '中专', '小学', '高中', '初中'],
    qualificationsColumnsList: []
  },
  deleteFamily(event) {
    let data = this.data.familyList;
    if (data && data.length == 1) {
      Toast('如删除将无法新增家庭关系！')
      return;
    } else {
      data.forEach((item, index) => {
        if (item.id == event.currentTarget.dataset.id) {
          data.splice(index, 1)
        }
      })
    }
    this.setData({
      familyList: data
    })
  },
  deleteEducate(event) {
    let data = this.data.educateList;
    if (data && data.length == 1) {
      Toast('至少填写一份学历信息！')
      return;
    } else {
      data.forEach((item, index) => {
        if (item.id == event.currentTarget.dataset.id) {
          data.splice(index, 1)
        }
      })
    }
    this.setData({
      educateList: data
    })
  },
  deletework(event) {
    let data = this.data.workList;
    if (data && data.length == 1) {
      Toast('无法删除!')
      return;
    } else {
      data.forEach((item, index) => {
        if (item.id == event.currentTarget.dataset.id) {
          data.splice(index, 1)
        }
      })
    }
    this.setData({
      workList: data
    })
  },
  familyNameInput(event) {
    let data = this.data.familyList;
    data.forEach((item) => {
      if (item.id == event.currentTarget.dataset.id) {
        item.name = event.detail
      }
    })
    this.setData({
      familyList: data
    })
  },
  familyorganizationInput(event) {
    let data = this.data.familyList;
    data.forEach((item) => {
      if (item.id == event.currentTarget.dataset.id) {
        item.organization = event.detail
      }
    })
    this.setData({
      familyList: data
    })
  },
  familyjobInput(event) {
    let data = this.data.familyList;
    data.forEach((item) => {
      if (item.id == event.currentTarget.dataset.id) {
        item.job = event.detail
      }
    })
    this.setData({
      familyList: data
    })
  },
  familyphoneInput(event) {
    let data = this.data.familyList;
    data.forEach((item) => {
      if (item.id == event.currentTarget.dataset.id) {
        item.phone = event.detail
      }
    })
    this.setData({
      familyList: data
    })
  },
  familybirthdayClick(event) {
    this.setData({
      familybirthdayShow: true,
      currentId: event.currentTarget.dataset.id
    })
  },
  backClick(event) {
    console.log(event);
    Dialog.confirm({
      title: '返回',
      message: '请确认是否返回？',
    }).then(() => {
      wx.navigateBack();
    }).catch(() => {
      console.log('取消')
    });
  },
  familybirthdayClose() {
    this.setData({
      familybirthdayShow: false,
    })
  },
  familybirthdayCancel() {
    this.setData({
      familybirthdayShow: false,
    })
  },
  familybirthdayConfirm(event) {
    let data = this.data.familyList;
    let time = this.formatDate(event.detail);
    data.forEach((item) => {
      if (Number(item.id) == Number(this.data.currentId)) {
        item.birthday = time
      }
    })
    this.setData({
      familyList: data,
      familybirthdayShow: false
    })
  },
  familyrelationClick(event) {
    console.log(event.currentTarget.dataset.id);
    this.setData({
      familyrelationShow: true,
      currentId: event.currentTarget.dataset.id
    })
  },
  familyrelationConfirm(event) {
    let self = this;
    console.log(event);
    let data = this.data.familyList;
    data.forEach((item) => {
      if (item.id == self.data.currentId) {
        item.relation = event.detail.value
      }
    })
    this.setData({
      familyList: data,
      familyrelationShow: false
    })
  },
  familyrelationCancel() {
    this.setData({
      familyrelationShow: false
    })
  },
  familyrelationClose() {
    this.setData({
      familyrelationShow: false
    })
  },
  addwork(event) {
    console.log(event);
    let obj = event.currentTarget.dataset.id;
    if (!obj.name || !obj.name.length || obj.starttime == '请选择' || obj.endtime == '请选择') {
      Toast('填完当前工作经历再新增！');
      return;
    }
    let data = this.data.workList;
    data.push({
      id: new Date().getTime(),
      name: '',
      starttime: '请选择',
      endtime: '请选择',
      qualifications: '请选择',
      special: '',
      reason: '',
    })
    this.setData({
      workList: data
    })
  },
  addEducate(event) {
    console.log(event);
    let obj = event.currentTarget.dataset.id;
    if (!obj.name || !obj.name.length || obj.starttime == '请选择' || obj.endtime == '请选择' || obj.qualifications == '请选择') {
      Toast('填完当前学历再新增！');
      return;
    }
    let data = this.data.educateList;
    data.push({
      id: new Date().getTime(),
      name: '',
      starttime: '请选择',
      endtime: '请选择',
      qualifications: '请选择',
      special: '',
      remark: '',
    })
    this.setData({
      educateList: data
    })
  },
  addFamily(event) {
    console.log(event);
    let obj = event.currentTarget.dataset.id;
    if (!obj.name || !obj.name.length || obj.relation == '请选择') {
      Toast('填完当前关系再新增！');
      return;
    }
    let data = this.data.familyList;
    data.push({
      id: new Date().getTime(),
      name: '',
      relation: '请选择',
      birthday: '请选择',
      organization: '',
      job: '',
      phone: ''
    })
    this.setData({
      familyList: data
    })
  },
  relationClick() {
    this.setData({
      relationShow: true
    })
  },
  relationCancel() {
    this.setData({
      relationShow: false
    })
  },
  relationClose() {
    this.setData({
      relationShow: false
    })
  },
  relationConfirm(event) {
    this.setData({
      ['familyinfo.relation']: event.detail.value,
      relationShow: false
    })
  },
  sexClose() {
    this.setData({
      sexShow: false
    })
  },
  sexCancel() {
    this.setData({
      sexShow: false
    })
  },
  sexClick() {
    this.setData({
      sexShow: true
    })
  },
  sexConfirm(event) {
    this.setData({
      ['familyinfo.sex']: event.detail.value,
      sexShow: false
    })
  },
  marryConfirm(event) {
    this.setData({
      ['baseinfo.marry']: event.detail.value,
      marryShow: false
    })
  },
  marryCancel() {
    this.setData({
      marryShow: false
    })
  },
  marryClose() {
    this.setData({
      marryShow: false
    })
  },
  marryClick() {
    this.setData({
      marryShow: true
    })
  },
  isFriendClick() {
    this.setData({
      isFriendShow: true
    })
  },
  isFriendClose() {
    this.setData({
      isFriendShow: false
    })
  },
  isFriendConfirm(event) {
    this.setData({
      ['baseinfo.isFriend']: event.detail.value,
      isFriendShow: false
    })
  },
  isFriendCancel() {
    this.setData({
      isFriendShow: false
    })
  },
  placeplaceClick() {
    let self = this;
    self.checkSessionMethod();
    Toast.loading({
      message: '加载中...',
      mask: true,
      duration: 0,
      forbidClick: true,
    });
    wx.request({
      url: `${ApiRootUrl}/labour/queryProvince`,
      method: 'post',
      header: {
        'content-type': 'application/json; charset=utf-8',
        'cookie': wx.getStorageSync("sessionid")
      },
      success(res) {
        Toast.clear();
        if (res.data.code == 20000) {
          let data = res.data.data;
          let placeplaceColumns = [{
              values: data,
              className: 'column1',
            },
            {
              values: data[0].children,
              className: 'column2',
            }
          ];
          self.setData({
            placeplaceColumns: placeplaceColumns,
            placeplaceShow: true
          })
        } else {
          Toast({
            duration: 3000,
            forbidClick: true,
            type: 'fail',
            message: res.data.message,
          });
        }
      },
      fail(res) {
        Toast.clear();
        Toast({
          duration: 3000,
          forbidClick: true,
          type: 'fail',
          message: '网络已断开',
        });
      }
    })

    // this.data.placeplaceColumns;
  },
  placeplaceClose() {
    this.setData({
      placeplaceShow: false
    })
  },
  placeplaceCancel() {
    this.setData({
      placeplaceShow: false
    })
  },
  placeplaceCancel() {
    this.setData({
      placeplaceShow: false
    })
  },
  placeplaceConfirm(event) {
    console.log(event.detail.value);
    this.setData({
      ['baseinfo.placeplace']: event.detail.value[0].text + "-" + event.detail.value[1].text,
      placeplaceShow: false
    })
  },
  placeplaceChange(event) {
    const {
      picker,
      value,
      index
    } = event.detail;
    console.log(value);
    picker.setColumnValues(1, value[0].children);
  },
  formatDate(date) {
    var date = new Date(date);
    var YY = String(date.getFullYear()) ;
    var MM = String((date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1)) ;
    var DD = String((date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate())) ;
    return YY + MM + DD;
  },
  birthdayConfirm(event) {
    let time = this.formatDate(event.detail);
    this.setData({
      ['baseinfo.birthday']: time,
      birthdayShow: false
    })
  },
  birthdayClose() {
    this.setData({
      birthdayShow: false
    })
  },
  birthdayCancel() {
    this.setData({
      birthdayShow: false
    })
  },
  birthdayClick() {
    this.setData({
      birthdayShow: true
    })
  },
  isStayClose() {
    this.setData({
      isStayShow: false
    })
  },
  isStayCancel() {
    this.setData({
      isStayShow: false
    })
  },
  isStayClick() {
    this.setData({
      isStayShow: true
    })
  },
  isStayConfirm(event) {
    console.log(event)
    this.setData({
      ['baseinfo.isStay']: event.detail.value,
      isStayShow: false
    })
  },
  nationConfirm(event) {
    this.setData({
      ['baseinfo.nation']: event.detail.value,
      nationShow: false
    })
  },
  nationCancel() {
    this.setData({
      nationShow: false
    })
  },
  nationClose() {
    this.setData({
      nationShow: false
    })
  },
  nationClick() {
    this.setData({
      nationShow: true
    })
  },
  familyjinji(event) {
    console.log(event);
    this.setData({
      ['familyinfo.name']: event.detail
    })
  },
  familyphonejinji(event) {
    this.setData({
      ['familyinfo.phone']: event.detail
    })
  },
  baseNameInput(event) {
    this.setData({
      ['baseinfo.username']: event.detail
    })
  },
  baseemailInput(event) {
    this.setData({
      ['baseinfo.email']: event.detail
    })
  },
  basestayInput(event) {
    this.setData({
      ['baseinfo.currentspace']: event.detail
    })
  },
  basehouseholdInput(event) {
    this.setData({
      ['baseinfo.household']: event.detail
    })
  },
  friendNameInput(event) {
    this.setData({
      ['baseinfo.friendName']: event.detail
    })
  },
  friendPhoneInput(event) {
    this.setData({
      ['baseinfo.friendPhone']: event.detail
    })
  },
  sexinfoClick() {
    this.setData({
      basesexShow: true
    })
  },
  basesexCancel() {
    this.setData({
      basesexShow: false
    })
  },
  basesexClose() {
    this.setData({
      basesexShow: false
    })
  },
  basesexConfirm(event) {
    this.setData({
      ['baseinfo.sex']: event.detail,
      basesexShow: false
    })
  },
  educatestartClick(event) {
    console.log(event.currentTarget.dataset.id)
    this.setData({
      currentId: event.currentTarget.dataset.id,
      educatestartShow: true
    })
  },
  workstartClick(event) {
    this.setData({
      currentId: event.currentTarget.dataset.id,
      workstartShow: true
    })
  },
  educatestartClose() {
    this.setData({
      educatestartShow: false
    })
  },
  workstartCancel() {
    this.setData({
      workstartShow: false
    })
  },
  workstartClose() {
    this.setData({
      workstartShow: false
    })
  },
  educatestartCancel() {
    this.setData({
      educatestartShow: false
    })
  },
  educateendClose() {
    this.setData({
      educateendShow: false
    })
  },
  workendClose() {
    this.setData({
      workendShow: false
    })
  },
  workendCancel() {
    this.setData({
      workendShow: false
    })
  },
  educateendCancel() {
    this.setData({
      educateendShow: false
    })
  },
  educateendConfirm(event) {
    let self = this;
    let data = this.data.educateList;
    let time = this.formatDate(event.detail);
    var year=time.substring(0,4)
    var month=time.substring(4,6)
    time=year+'-'+month
    data.forEach((item) => {
      if (String(item.id) == String(self.data.currentId)) {
        item.endtime = time
      }
    })
    this.setData({
      educateList: data,
      educateendShow: false
    })
  },
  workendConfirm(event) {
    let self = this;
    let data = this.data.workList;
    let time = this.formatDate(event.detail);
    var year=time.substring(0,4)
    var month=time.substring(4,6)
    time=year+'-'+month
    data.forEach((item) => {
      if (String(item.id)  ==String(self.data.currentId) ) {
        item.endtime = time
      }
    })
    this.setData({
      workList: data,
      workendShow: false
    })
  },
  qualificationsClick(event) {
    this.setData({
      currentId: event.currentTarget.dataset.id,
      qualificationsShow: true
    })
  },
  workqualificationsClick(event) {
    this.setData({
      currentId: event.currentTarget.dataset.id,
      workqualificationsShow: true
    })
  },
  workqualificationsCancel() {
    this.setData({
      workqualificationsShow: false
    })
  },
  workqualificationsClose() {
    this.setData({
      workqualificationsShow: false
    })
  },
  workqualificationsConfirm(event) {
    let data = this.data.workList;
    data.forEach((item) => {
      if (item.id == this.data.currentId) {
        item.qualifications = event.detail.value
      }
    })
    this.setData({
      workList: data,
      workqualificationsShow: false
    })
  },
  qualificationsClose() {
    this.setData({
      qualificationsShow: false
    })
  },
  qualificationsCancel() {
    this.setData({
      qualificationsShow: false
    })
  },
  qualificationsConfirm(event) {
    console.log(event)
    let data = this.data.educateList;
    data.forEach((item) => {
      if (item.id == this.data.currentId) {
        item.qualifications = event.detail.value
      }
    })
    this.setData({
      educateList: data,
      qualificationsShow: false
    })
  },
  educatestartConfirm(event) {
    let self = this;
    let data = this.data.educateList;
    let time = this.formatDate(event.detail);
    var year=time.substring(0,4)
    var month=time.substring(4,6)
    time=year+'-'+month
    data.forEach((item) => {
      if (String(item.id) == String(self.data.currentId)) {
        item.starttime = time
      }
    })
    this.setData({
      educateList: data,
      educatestartShow: false
    })
  },
  workstartConfirm(event) {
    let self = this;
    let data = this.data.workList;
    let time = this.formatDate(event.detail);
    var year=time.substring(0,4)
    var month=time.substring(4,6)
    time=year+'-'+month
    data.forEach((item) => {
      if (String(item.id)  == String(self.data.currentId) ) {
        item.starttime = time
      }
    })
    this.setData({
      workList: data,
      workstartShow: false
    })
  },
  schoolNameInput(event) {
    let data = this.data.educateList;
    data.forEach((item) => {
      if (item.id == event.currentTarget.dataset.id) {
        item.name = event.detail
      }
    })
    this.setData({
      educateList: data
    })
  },
  companyNameInput(event) {
    let data = this.data.workList;
    data.forEach((item) => {
      if (item.id == event.currentTarget.dataset.id) {
        item.name = event.detail
      }
    })
    this.setData({
      workList: data
    })
  },
  remarkInput(event) {
    let data = this.data.educateList;
    data.forEach((item) => {
      if (item.id == event.currentTarget.dataset.id) {
        item.remark = event.detail
      }
    })
    this.setData({
      educateList: data
    })
  },
  specialInput(event) {
    let data = this.data.educateList;
    data.forEach((item) => {
      if (item.id == event.currentTarget.dataset.id) {
        item.special = event.detail
      }
    })
    this.setData({
      educateList: data
    })
  },
  workspecialInput(event) {
    let data = this.data.workList;
    data.forEach((item) => {
      if (item.id == event.currentTarget.dataset.id) {
        item.special = event.detail
      }
    })
    this.setData({
      workList: data
    })
  },
  reasonInput(event) {
    let data = this.data.workList;
    data.forEach((item) => {
      if (item.id == event.currentTarget.dataset.id) {
        item.reason = event.detail
      }
    })
    this.setData({
      workList: data
    })
  },
  educateendClick(event) {
    this.setData({
      currentId: event.currentTarget.dataset.id,
      educateendShow: true
    })
  },
  workendClick(event) {
    this.setData({
      currentId: event.currentTarget.dataset.id,
      workendShow: true
    })
  },
  tabClick(event){
    this.setData({
      active: event.detail.index,
    })
  },
  submitBase1() {
    var self = this;
    Toast.loading({
      message: '加载中...',
      mask: true,
      duration: 0,
      forbidClick: true,
    });
    var baseinfo = self.data.baseinfo;
    let nation = '';
    self.data.nationColumnsList.forEach((item) => {
      if (item.dictionaryName == baseinfo.nation) {
        nation = item.dictionaryCode;
      }
    })
    let sex = '';
    if (baseinfo.sex == '男') {
      sex = '1';
    } else if (baseinfo.sex == '女') {
      sex = '2';
    } else {
      sex = '0';
    }
    let isStay = '';
    if (baseinfo.isStay == '是') {
      isStay = '1';
    } else {
      isStay = '0';
    }
    let isFriend = '';
    if (baseinfo.isFriend == '有') {
      isFriend = '1';
    } else {
      isFriend = '0';
    }
    let marry = '';
    if (baseinfo.marry == '已婚') {
      marry = '20';
    } else {
      marry = '10';
    }
    let params = {
      type: 0,
      employeeInfo: {
        name: baseinfo.username,
        identity: baseinfo.identifycode,
        phone: baseinfo.phone,
      },
      employeeDetails: {
        id: baseinfo.id,
        province: baseinfo.placeplace.split("-")[0],
        city: baseinfo.placeplace.split("-")[1],
        email: baseinfo.email,
        presentAddr: baseinfo.currentspace,
        sex: sex,
        birthday: baseinfo.birthday,
        nation: nation,
        nativePlace: baseinfo.placeplace,
        isDorm: isStay,
        hasFriend: isFriend,
        friendName: baseinfo.friendName,
        friendPhone: baseinfo.friendPhone,
        nativeAddr: baseinfo.household,
        isMarry: marry,
      }
    }
    console.log(params);
    wx.request({
      url: `${ApiRootUrl}/labour/addEmployDetails`,
      method: 'post',
      header: {
        'content-type': 'application/json; charset=utf-8',
        'cookie': wx.getStorageSync("sessionid")
      },
      params: params,
      data: params,
      success(res) {
        Toast.clear();
        if (res.data.code == 20000) {
          console.log(res.data.data);
          Toast.success('基础信息提交成功！');
          self.setData({
            active: 1,
            ['tabList[0].disabled']: true,
            ['tabList[1].disabled']: false,
            ['tabList[2].disabled']: true,
            ['tabList[3].disabled']: true,
          })
        } else {
          Toast({
            duration: 3000,
            forbidClick: true,
            type: 'fail',
            message: res.data.message,
          });
        }
      },
      fail(res) {
        Toast.clear();
        Toast({
          duration: 3000,
          forbidClick: true,
          type: 'fail',
          message: '网络已断开',
        });
      }
    })
  },
  submitBase2() {
    console.log(this.data.familyinfo);
    console.log(this.data.familyList);
    let self = this;
    Toast.loading({
      message: '加载中...',
      mask: true,
      duration: 0,
      forbidClick: true,
    });
    let familyinfo = JSON.parse(JSON.stringify(self.data.familyinfo));
    let sex = '';
    if (familyinfo.sex == '男') {
      sex = '1';
    } else if (familyinfo.sex == '女') {
      sex = '2';
    } else {
      sex = '0';
    }
    let relation = '';
    if (familyinfo.relation == '父母') {
      relation = '0';
    } else if (familyinfo.relation == '配偶') {
      relation = '1';
    } else if (familyinfo.relation == '子女') {
      relation = '2';
    } else if (familyinfo.relation == '兄弟姐妹') {
      relation = '3';
    } else {
      relation = "";
    }
    let flag = false;
    let flagPhone = false;
    let familyList = JSON.parse(JSON.stringify(self.data.familyList));
    familyList.forEach((item) => {
      console.log("item===>", item)
      let reg2 = /^1(3|4|5|6|7|8|9)\d{9}$/
      if (item.phone != null) {
        if (!reg2.test(item.phone)) {
          flagPhone = true
        }
      }

      if (!isChinese(item.name)) {
        flag = true
      }
      if (item.relation == '父母') {
        item.familyRelative = '0'
      } else if (item.relation == '配偶') {
        item.familyRelative = '1'
      } else if (item.relation == '子女') {
        item.familyRelative = '2'
      } else if (item.relation == '兄弟姐妹') {
        item.familyRelative = '3'
      } else if (item.relation == '请选择') {
        flag = true
        item.familyRelative = "";
      }
    })
    if (flag) {
      Toast('请检查家庭关系里面姓名与本人关系是否都填写')
      return
    }
    if (flagPhone) {
      Toast('请检查家庭关系里面联系电话是否正确')
      return
    }
    let familyList1 = [];
    familyList.forEach((item) => {
      familyList1.push({
        id: item.id,
        familyName: item.name,
        familyBirth: item.birthday == "请选择" ? null : item.birthday,
        componentName: item.organization,
        familyPosition: item.job,
        familyPhone: item.phone,
        familyRelative: item.familyRelative,
        openId: wx.getStorageSync("openId")
      })
    })
    if (familyList1 && familyList1.length == 1) {
      if (familyList1[0].familyName == '') {
        familyList1 = [];
      }
    } else {
      familyList1.forEach((item, index) => {
        if (!item.familyName || !item.familyName.length) {
          familyList1.splice(index, 1)
        }
      })
    }

    let params = {
      contactList: [{
        familyName: familyinfo.name,
        familySex: sex,
        familyRelative: relation,
        familyPhone: familyinfo.phone,
        openId: wx.getStorageSync("openId")
      }],
      familyList: familyList1
    }
    console.log(params)
    // return
    wx.request({
      url: `${ApiRootUrl}/labour/addEmployFamily`,
      method: 'post',
      header: {
        'content-type': 'application/json; charset=utf-8',
        'cookie': wx.getStorageSync("sessionid")
      },
      params: params,
      data: params,
      success(res) {
        Toast.clear();
        if (res.data.code == 20000) {
          Toast.success('家庭信息提交成功！');
          self.setData({
            active: 2,
            ['tabList[0].disabled']: true,
            ['tabList[1].disabled']: true,
            ['tabList[2].disabled']: false,
            ['tabList[3].disabled']: true,
          })
        } else {
          Toast({
            duration: 3000,
            forbidClick: true,
            type: 'fail',
            message: res.data.message,
          });
        }
      },
      fail(res) {
        Toast.clear();
        Toast({
          duration: 3000,
          forbidClick: true,
          type: 'fail',
          message: '网络已断开',
        });
      }
    })
  },
  submitBase3() {
    let self = this;
    let flagTime = false;
    let educationList = JSON.parse(JSON.stringify(self.data.educateList));
    educationList.forEach((item1) => {
      console.log("item1==>", item1)
      flagTime = judgeData(item1.starttime, item1.endtime)
      self.data.qualificationsColumnsList.forEach((item2) => {
        if (item1.qualifications == item2.dictionaryName) {
          item1.dictionaryCode = item2.dictionaryCode
        }
      })
    })
    if(flagTime){
      Toast('入学日期不能大于毕业日期')
      return
    }
    console.log("flagTime",)
    let educationList1 = [];
    educationList.forEach((item) => {
      educationList1.push({
        id: item.id,
        beginTime: item.starttime,
        endTime: item.endtime,
        schoolName: item.name,
        major: item.special,
        educationLevel: Number(item.dictionaryCode),
        remark: item.remark,
        openId: wx.getStorageSync("openId")
      })
    })
    educationList1.forEach((item, index) => {
      if (!item.schoolName || !item.schoolName.length) {
        educationList1.splice(index, 1)
      }
    })
    let params = educationList1;
    Toast.loading({
      message: '加载中...',
      mask: true,
      duration: 0,
      forbidClick: true,
    });
    wx.request({
      url: `${ApiRootUrl}/labour/addEmployEducation`,
      method: 'post',
      header: {
        'content-type': 'application/json; charset=utf-8',
        'cookie': wx.getStorageSync("sessionid")
      },
      params: params,
      data: params,
      success(res) {
        Toast.clear();
        if (res.data.code == 20000) {
          self.setData({
            active: 3,
            ['tabList[0].disabled']: true,
            ['tabList[1].disabled']: true,
            ['tabList[2].disabled']: true,
            ['tabList[3].disabled']: false,
          })
        } else {
          Toast({
            duration: 3000,
            forbidClick: true,
            type: 'fail',
            message: res.data.message,
          });
        }
      },
      fail(res) {
        Toast.clear();
        Toast({
          duration: 3000,
          forbidClick: true,
          type: 'fail',
          message: '网络已断开',
        });
      }
    })
  },
  submitBase4() {
    let self = this;
    let flagTime = false
    let workList = JSON.parse(JSON.stringify(self.data.workList));
    workList.forEach((item) => {
      console.log("item===>",item)
      if(item.starttime&&item.endTime){
         flagTime = judgeData(item.starttime, item.endtime)
      }
     
      if (item.qualifications == '是') {
        item.qualifications = '1';
      } else {
        item.qualifications = '0';
      }
    })
    // if(flagTime){
    //   Toast('开始日期不能大于结束日期')
    //   return
    // }
    let workList1 = [];
    workList.forEach((item) => {
      workList1.push({
        id: item.id,
        beginTime: item.starttime,
        endTime: item.endtime,
        companyName: item.name,
        position: item.special,
        leaveReason: item.reason,
        hasCertify: item.qualifications,
        openId: wx.getStorageSync("openId")
      })
    })
    workList1.forEach((item, index) => {
      if (!item.companyName || !item.companyName.length) {
        workList1.splice(index, 1)
      }
    })
    let params = workList1
    Toast.loading({
      message: '加载中...',
      mask: true,
      duration: 0,
      forbidClick: true,
    });
    wx.request({
      url: `${ApiRootUrl}/labour/addEmployWork`,
      method: 'post',
      header: {
        'content-type': 'application/json; charset=utf-8',
        'cookie': wx.getStorageSync("sessionid")
      },
      params: params,
      data: params,
      success(res) {
        Toast.clear();
        if (res.data.code == 20000) {
          Toast({
            duration: 3000,
            forbidClick: true,
            type: 'success',
            message: '提交成功！',
          });
          wx.navigateTo({
            url: `/pages/firstpage/firstpage?isLogin=${0}`,
          })
        } else {
          Toast({
            duration: 3000,
            forbidClick: true,
            type: 'fail',
            message: res.data.message,
          });
        }
      },
      fail(res) {
        Toast.clear();
        Toast({
          duration: 3000,
          forbidClick: true,
          type: 'fail',
          message: '网络已断开',
        });
      }
    })
    this.setData({
      active: 1,
      ['tabList[0].disabled']: true,
      ['tabList[1].disabled']: false,
      ['tabList[2].disabled']: true,
      ['tabList[3].disabled']: true,
    })
  },
  // 点击下一步
  submitClick(event) {
    this.checkSessionMethod();
    if (this.data.active == 0) {
      if (!this.data.baseinfo.username || !this.data.baseinfo.username.length) {
        Toast('请输入姓名！')
        return;
      }
      // let reg = /^1(3|4|5|6|7|8|9)\d{9}$/
      // if (!reg.test(this.data.baseinfo.phone)) {
      if (!this.data.baseinfo.phone) {
        Toast('请输入手机号码！')
        return;
      }
      // let reg1 = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
      // if (!reg1.test(this.data.baseinfo.email)) {
      //   if (!this.data.baseinfo.email) { 
      //   Toast('请输入邮箱！')
      //   return;
      // }
      // let reg2 = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/
      // if (!reg2.test(this.data.baseinfo.identifycode)) {
      if (!this.data.baseinfo.identifycode) {
        Toast('请输入身份证号！')
        return;
      }

      if (this.data.baseinfo.nation == '请选择') {
        Toast('请选择民族！')
        return;
      }
      if (this.data.baseinfo.marry == '请选择') {
        Toast('请选择婚姻状态！')
        return;
      }

      if (this.data.baseinfo.placeplace == '请选择') {
        Toast('请选择籍贯！')
        return;
      }
      if (this.data.baseinfo.household == "") {
        Toast('请输入户籍地址！')
        return;
      }
      if (this.data.baseinfo.currentspace == "") {
        Toast('请输入现住地！')
        return;
      }
      this.submitBase1();
    } else if (this.data.active == 1) {
      // if (!isChinese(this.data.familyinfo.name)) {
      //   Toast('请输入正确紧急联系人姓名')
      //   return;
      // }
      if (this.data.familyinfo.sex == '请选择') {
        Toast('请选择紧急联系人性别')
        return;
      }
      if (this.data.familyinfo.relation == '请选择') {
        Toast('请选择与本人关系')
        return;
      }
      // let reg2 = /^1(3|4|5|6|7|8|9)\d{9}$/
      // if (!reg2.test(this.data.familyinfo.phone)) {
      if (!this.data.familyinfo.phone) {
        Toast('请检查联系人联系方式是否正确')
        return;
      }
      this.submitBase2();
    } else if (this.data.active == 2) {
      let obj = this.data.educateList;
      console.log("obj------:", obj);
      // if (this.judgeArrName("name", obj, "")) {
      //   Toast('有学校名称未填写正确！')
      //   return
      // }
      if (this.judgeArrValue("endtime", obj, "请选择")) {
        Toast('有毕业时间未选！')
        return
      }
      if (this.judgeArrValue("starttime", obj, "请选择")) {
        Toast('有入学时间未选！')
        return
      }
      if (this.judgeArrValue("qualifications", obj, "请选择")) {
        Toast('有学历未选！')
        return
      }
      // if(obj && obj.length == 1){
      //   if(!obj[0].name || !obj[0].name.length || obj[0].starttime == '请选择'|| obj[0].endtime == '请选择' || obj[0].qualifications == '请选择' ){
      //     Toast('请填写至少一个学历信息！');
      //     return;
      //   }
      // }
      // let dataList = this.data.educateList;
      // dataList.forEach((item,index)=>{
      //   if(!item.name || !item.name.length || item.starttime == '请选择'|| item.endtime == '请选择' || item.qualifications == '请选择' ){
      //     dataList.splice(index,1)
      //   }
      // })
      this.submitBase3();

    } else if (this.data.active == 3) {
      let obj = this.data.workList
      console.log(obj)
      // if(this.judgeArrValue("endtime", obj, "请选择")){
      //   Toast('有结束时间未选！')
      //   return
      // }
      // if(this.judgeArrValue("starttime", obj, "请选择")){
      //   Toast('有开始时间未选！')
      //   return
      // }
      // if(this.judgeArrName("name", obj, "")){
      //   Toast('有单位名称未填写正确！')
      //   return
      // }
      this.submitBase4();
    }
  },
  //判断一个数组里面的的一个参数是否为空
  judgeArrValue(params, arr, judgeParams) {
    let flag = false
    for (let i = 0; i < arr.length; i++) {
      if (arr[i][params] == judgeParams) {
        flag = true
      }
    }
    return flag
  },
  promiseChange(event) {
    this.setData({
      promiseRadio: event.detail,
      submitFlag: !event.detail
    });
  },
  checkSessionMethod() {
    let self = this;
    let flag = true;
    wx.checkSession({
      success: function (res) {
        console.log('session_key未过期')
        //session_key未过期
        flag = true;
      },
      fail: function (res) {
        flag = false;
        self.setData({
          tishiShow: true
        })
      }
    })
    if (!flag) {
      return;
    }
  },
  judgeArrName(params, arr, judgeParams){
    let flag = false
    for (let i = 0; i < arr.length; i++) {
      console.log("--->",arr[i][params])
      if (!isChinese(arr[i][params])) {
        flag = true
      }
    }
    return flag
  },
  initDataList() {
    let self = this;
    self.checkSessionMethod();
    let openId = wx.getStorageSync("openId");
    let url = `${ApiRootUrl}/labour/detailsEmployee/${openId}/0`;
    Toast.loading({
      message: '加载中...',
      mask: true,
      duration: 0,
      forbidClick: true,
    });
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/json; charset=utf-8',
        'cookie': wx.getStorageSync("sessionid")
      },
      success(res) {
        Toast.clear();
        if (res.data.code == 20000) {
          let employeeInfo = res.data.data.employeeInfo || {};
          let employeeDetails = res.data.data.employeeDetails || {};
          let contactList = res.data.data.contactList || [];
          let familyList = [];
          let familyList1 = res.data.data.familyList || [{
            id: new Date().getTime(),
            familyName: '',
            familyRelativeDesc: '请选择',
            familyBirth: '请选择',
            componentName: '',
            familyPosition: '',
            familyPhone: ''
          }];
          familyList1.forEach((item) => {
            familyList.push({
              id: item.id,
              name: item.familyName,
              birthday: item.familyBirth == null ? "请选择" : item.familyBirth,
              organization: item.componentName,
              job: item.familyPosition,
              phone: item.familyPhone,
              relation: item.familyRelativeDesc == null ? "请选择" : item.familyRelativeDesc
            })
          })
          let educateList = [];
          let educationList = res.data.data.educationList || [{
            id: new Date().getTime(),
            schoolName: '',
            beginTime: '请选择',
            endTime: '请选择',
            educationLevelDesc: '请选择',
            major: '',
            remark: '',
            dictionaryCode:"",
          }];
          educationList.forEach((item) => {
            educateList.push({
              id: item.id,
              starttime: item.beginTime,
              endtime: item.endTime,
              name: item.schoolName,
              special: item.major,
              dictionaryCode:item.educationLevel,
              qualifications: item.educationLevelDesc,
              remark: item.remark
            })
          })
          let workList = []
          let workList1 = res.data.data.workList || [{
            id: new Date().getTime(),
            companyName: '',
            beginTime: '请选择',
            endTime: '请选择',
            hasCertifyDesc: '请选择',
            position: '',
            leaveReason: '',
            
          }];
          workList1.forEach((item) => {
            workList.push({
              id: item.id,
              starttime: item.beginTime,
              endtime: item.endTime,
              name: item.companyName,
              special: item.position,
              reason: item.leaveReason,
              qualifications: item.hasCertifyDesc,
            })
          })
          let familyinfo = {};
          if (contactList && contactList.length) {
            familyinfo = {
              name: contactList[0].familyName || '',
              sex: contactList[0].familySexDesc || '请选择',
              relation: contactList[0].familyRelativeDesc || '请选择',
              phone: contactList[0].familyPhone || ''
            }
          } else {
            familyinfo = {
              name: '',
              sex: '请选择',
              relation: '请选择',
              phone: ''
            }
          }
          let baseinfo = {
            id: employeeDetails.id,
            currentspace: employeeDetails.presentAddr || '',
            username: employeeInfo.name || '',
            email: employeeDetails.email || '',
            sex: employeeDetails.sexDesc || '',
            phone: employeeInfo.phone || '',
            identifycode: employeeInfo.identity || '',
            birthday: employeeDetails.birthday || '',
            nation: employeeDetails.nation || '请选择',
            placeplace: employeeDetails.nativePlace || '请选择',
            isStay: employeeDetails.isDormDesc || '请选择',
            isFriend: employeeDetails.hasFriendDesc || '无',
            friendName: employeeDetails.friendName || '',
            friendPhone: employeeDetails.friendPhone || '',
            household: employeeDetails.nativeAddr || '',
            marry: employeeDetails.isMarryDesc || '请选择'
          }
          
          self.setData({
            baseinfo: baseinfo,
            familyinfo: familyinfo,
            familyList: familyList,
            educateList: educateList,
            workList: workList
          })
        } else {
          Toast({
            duration: 3000,
            forbidClick: true,
            type: 'fail',
            message: res.data.message,
          });
        }
      },
      fail(res) {
        Toast.clear();
        Toast({
          duration: 3000,
          forbidClick: true,
          type: 'fail',
          message: '网络已断开',
        });
      }
    })
  },
  initMZ() {
    let self = this;
    self.checkSessionMethod();
    let params = {
      dictionaryType: 'nation'
    }
    let url = `${ApiRootUrl}/dictionaries/querytbDictionariesList`;
    Toast.loading({
      message: '加载中...',
      mask: true,
      duration: 0,
      forbidClick: true,
    });
    wx.request({
      url: url,
      method: 'get',
      header: {
        'content-type': 'application/json; charset=utf-8',
        'cookie': wx.getStorageSync("sessionid")
      },
      parmas: params,
      data: params,
      success(res) {
        Toast.clear();
        if (res.data.code == 20000) {
          let nationColumnsList = res.data.data;
          let nationColumns = nationColumnsList.map(value => value['dictionaryName']);
          self.setData({
            nationColumns: nationColumns,
            nationColumnsList: nationColumnsList
          })
        } else {
          Toast({
            duration: 3000,
            forbidClick: true,
            type: 'fail',
            message: res.data.message,
          });
        }
      },
      fail(res) {
        Toast.clear();
        Toast({
          duration: 3000,
          forbidClick: true,
          type: 'fail',
          message: '网络已断开',
        });
      }
    })
  },
  initXL() {
    let self = this;
    self.checkSessionMethod();
    let params = {
      dictionaryType: 'education'
    }
    let url = `${ApiRootUrl}/dictionaries/querytbDictionariesList`;
    Toast.loading({
      message: '加载中...',
      mask: true,
      duration: 0,
      forbidClick: true,
    });
    wx.request({
      url: url,
      method: 'get',
      header: {
        'content-type': 'application/json; charset=utf-8',
        'cookie': wx.getStorageSync("sessionid")
      },
      parmas: params,
      data: params,
      success(res) {
        Toast.clear();
        if (res.data.code == 20000) {
          let qualificationsColumnsList = res.data.data;
          let qualificationsColumns = qualificationsColumnsList.map(value => value['dictionaryName']);
          self.setData({
            qualificationsColumns: qualificationsColumns,
            qualificationsColumnsList: qualificationsColumnsList
          })
        } else {
          Toast({
            duration: 3000,
            forbidClick: true,
            type: 'fail',
            message: res.data.message,
          });
        }
      },
      fail(res) {
        Toast.clear();
        Toast({
          duration: 3000,
          forbidClick: true,
          type: 'fail',
          message: '网络已断开',
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      tabHeight:wx.getSystemInfoSync()['statusBarHeight']
    })
    
    this.initDataList();
    this.initMZ();
    this.initXL();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
      var query = wx.createSelectorQuery();
      query.select('.tab-content').boundingClientRect(rect => {
      let height = rect.height;
      this.setData({
        tabHeight:height
      })
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log('1111111')
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})